package com.atguigu.spring5.dao;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author dengzhao
 * @create 2021-12-08 21:58
 */
public class JDKProxy {


    public static void main(String[] args) {


        Class intefaces[] ={UserDao.class};

        UserDaoImpl userDao = new UserDaoImpl();
        UserDao dao = (UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), intefaces, new UserDaoProxy(userDao));
        int add = dao.add(1, 1);
        System.out.println(add);
        System.out.println("hello git!");
        System.out.println("hello git!");
    }
}

class UserDaoProxy implements InvocationHandler {

    private Object obj;
    public UserDaoProxy(Object obj){
        this.obj=obj;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("方法执行之前"+method.getName()+"参数："+args);

        Object res = method.invoke(obj, args);

        System.out.println("方法执行之后"+obj);


        return res;
    }
}

